package com.bis.ymyy.controller;

import com.bis.ymyy.common.utils.QueryPage;
import com.bis.ymyy.common.utils.Result;
import com.bis.ymyy.entity.Appointment;
import com.bis.ymyy.entity.History;
import com.bis.ymyy.service.AppointmentService;
import com.bis.ymyy.service.HistoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

@RestController
@RequestMapping("/appointment")
@Api(tags = "/预约相关接口")
public class AppointmentController {

    @Autowired(required = false)
    private AppointmentService appointmentService;
    @Autowired(required = false)
    private HistoryService historyService;

    @ApiOperation("/查询所有预约")
    @GetMapping("/findAllAppointment")
    public Result<List> findAllAppointment() {
        Result<List> result = new Result<>();
        List<Appointment> appointmentList = appointmentService.findAll();
        result.setCode(200);
        result.setMsg("查询成功");
        result.setSuccess(true);
        result.setData(appointmentList);
        return result;
    }

    @ApiOperation("/分页查询")
    @PostMapping("/queryPageAppoint")
    public Result<PageInfo> queryPageAppoint(@RequestBody(required = false) QueryPage page){
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<Appointment> models = appointmentService.findAll();
        PageInfo<Appointment> pageInfo = new PageInfo<>(models);
        Result<PageInfo> result = new Result<>();
        result.setCode(200);
        result.setSuccess(true);
        result.setMsg("success");
        result.setData(pageInfo);
        return result;
    }

    @ApiOperation("/用户名查询预约")
    @GetMapping("/findOneAppointment")
    public Result<Appointment> findOneAppointment(@RequestParam String userName) {
        Result<Appointment> result = new Result<>();
        Appointment appointment = appointmentService.findByUserName(userName);
        if (appointment == null) {
            result.setMsg("您还没有预约");
            result.setCode(200);
            result.setData(null);
            result.setSuccess(true);
        } else {
            result.setMsg("成功");
            result.setCode(200);
            result.setSuccess(true);
            result.setData(appointment);
        }
        return result;
    }

    @ApiOperation("/新增预约")
    @PostMapping("/addOneAppointment")
    public Result<Appointment> addOneAppointment(@RequestBody Appointment appointment) {
        Result<Appointment> result = new Result<>();
        result.setMsg("预约失败");
        result.setCode(400);
        if (appointment == null) {
            result.setMsg("参数为空");
            result.setCode(400);
            result.setSuccess(false);
            result.setData(null);
        } else {
            if (appointmentService.findByUserName(appointment.getUserName()) != null) {
                result.setCode(400);
                result.setSuccess(false);
                result.setMsg("请不要重复预约");
            } else {
                History history = historyService.getOne(appointment.getUserName());
                if (history != null) {
                    if (history.getSecondPin() != 0) {
                        result.setCode(400);
                        result.setSuccess(false);
                        result.setMsg("您已接种,无需预约");
                    } else {
                        if (appointment.getWhichPin().equals("第一针")) {
                            result.setCode(400);
                            result.setSuccess(false);
                            result.setMsg("您已接种过第一针,请预约第二针");
                        } else {
                            if (appointmentService.addOne(appointment) == 1) {
                                result.setCode(200);
                                result.setMsg("预约成功");
                                result.setSuccess(true);
                            }
                        }
                    }
                } else {
                    if (appointmentService.addOne(appointment) == 1) {
                        result.setCode(200);
                        result.setMsg("预约成功");
                        result.setSuccess(true);
                    }
                }
            }
        }
        return result;
    }

    @ApiOperation("/取消预约")
    @GetMapping("/cancelAppointment")
    public Result<Appointment> cancelAppointment(@RequestParam String userName) {
        Result<Appointment> result = new Result<>();
        result.setMsg("取消预约失败");
        result.setCode(400);
        if (userName.equals("")) {
            result.setMsg("参数为空");
            result.setCode(400);
            result.setSuccess(false);
            result.setData(null);
        } else {
            if (appointmentService.findByUserName(userName) == null) {
                result.setCode(200);
                result.setSuccess(true);
                result.setMsg("您还未预约");
            } else {
                appointmentService.cancelOne(userName);
                result.setCode(200);
                result.setMsg("取消预约成功");
                result.setSuccess(true);
            }
        }
        return result;
    }

    @ApiOperation("/根据日期查询预约")
    @GetMapping("/findByDate")
    public Result<List> findByDate(@RequestParam Date date) {
        Result<List> result = new Result<>();
        result.setMsg("查询预约失败");
        result.setCode(400);
        if (date == null) {
            result.setMsg("参数为空");
            result.setCode(400);
            result.setSuccess(false);
        } else {
            List<Appointment> appointmentList = appointmentService.findByDate(date);
            if (appointmentList == null) {
                result.setSuccess(false);
                result.setCode(400);
                result.setMsg("该日期没有预约");
            } else {
                result.setMsg("查询成功");
                result.setCode(200);
                result.setSuccess(true);
                result.setData(appointmentList);
            }
        }
        return result;
    }

    @ApiOperation(value = "修改预约")
    @PostMapping("/modifyAppointment")
    public Result modifyAppointment(@RequestBody Appointment appointment) {
        Result result = new Result();
        if (appointment == null ) {
            result.setCode(400);
            result.setMsg("传参为空");
            result.setSuccess(false);
        } else {
//            String whichPin = appointment.getWhichPin();
//            if(historyService.getOne(appointment.getUserName()) != null){
//                int isFirstPin = historyService.getOne(appointment.getUserName()).getFirstPin();
//                if (isFirstPin == 1 && whichPin.equals("第一针")) {
//                    result.setCode(400);
//                    result.setMsg("您已接种过第一针，请预约第二针");
//                    result.setSuccess(false);
//                } else {
//                    appointmentService.modifyAppointment(appointment);
//                    result.setSuccess(true);
//                    result.setCode(200);
//                    result.setMsg("修改成功");
//                }
//            } else {
                appointmentService.modifyAppointment(appointment);
                result.setSuccess(true);
                result.setCode(200);
                result.setMsg("修改成功");
//            }
        }
        return result;
    }
}
